const Joi = require('joi');
const router = require('koa-router')();
const validationMiddleware = require('../../middleware/validate');

const { dealNotEmpty } = require("../../utils/dberrordeal");
const MenuController = require('../../controllers/system/menuController');

const rulesSchema = {
    '/addmenu': Joi.object({
        name: dealNotEmpty('路由名称'),
    }),
    '/putmenu': Joi.object({
        name: dealNotEmpty('路由名称'),
        id: dealNotEmpty('路由id'),
    }),
    '/getmenud': Joi.object({
        id: dealNotEmpty('路由id'),
    }),
    '/addbtnrole': Joi.object({
        menuId: dealNotEmpty('菜单id'),
        color: dealNotEmpty('按钮颜色'),
        name: dealNotEmpty('按钮名称'),
        code: dealNotEmpty('按钮标识'),
    }),
    '/getbtnrole': Joi.object({
        id: dealNotEmpty('菜单id'),
    }),
    '/putbtnrole': Joi.object({
        data: Joi.object({
            menuId: dealNotEmpty('菜单id'),
            color: dealNotEmpty('按钮颜色'),
            name: dealNotEmpty('按钮名称'),
            code: dealNotEmpty('按钮标识'),
        }),
        did: dealNotEmpty('按钮id'),
    }),
    '/deletebtnrole': Joi.object({
        id: dealNotEmpty('菜单id'),
        did: dealNotEmpty('按钮id'),
    }),
    '/delmenu': Joi.object({
        id: dealNotEmpty('路由id'),
    }),
}

router.post('/addbtnrole', validationMiddleware(rulesSchema), MenuController.addBtnRole);
router.post('/getbtnrole', MenuController.getBtnRole);
router.post('/putbtnrole', MenuController.putBtnRole);
router.post('/deletebtnrole', MenuController.deleteBtnRole);
router.post('/addmenu', validationMiddleware(rulesSchema), MenuController.addMenu);
router.post('/putmenu', validationMiddleware(rulesSchema), MenuController.putMenu);
router.post('/getmenud', validationMiddleware(rulesSchema), MenuController.getMenuDetail);
router.post('/getmenulist', MenuController.getMenuList);
router.post('/delmenu', MenuController.deleteMenu);

module.exports = router;

/**
 * @swagger
 * tags:
 *   name: 菜单管理
 *   description: 菜单和按钮权限相关接口
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     Menu:
 *       type: object
 *       properties:
 *         _id:
 *           type: string
 *           example: 507f1f77bcf86cd799439011
 *         name:
 *           type: string
 *           example: 用户管理
 *         path:
 *           type: string
 *           example: /user
 *         alias:
 *           type: string
 *           example: user
 *         disabled:
 *           type: boolean
 *           example: false
 *         isMenu:
 *           type: boolean
 *           example: true
 *         order:
 *           type: number
 *           example: 1
 *         parentId:
 *           type: string
 *           example: 0
 *         btnRoles:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/ButtonRole'
 *     ButtonRole:
 *       type: object
 *       properties:
 *         _id:
 *           type: string
 *           example: 507f1f77bcf86cd799439012
 *         menuId:
 *           type: string
 *           example: 507f1f77bcf86cd799439011
 *         color:
 *           type: string
 *           example: primary
 *         name:
 *           type: string
 *           example: 新增
 *         code:
 *           type: string
 *           example: add
 *     MenuTree:
 *       type: object
 *       properties:
 *         _id:
 *           type: string
 *         name:
 *           type: string
 *         path:
 *           type: string
 *         alias:
 *           type: string
 *         disabled:
 *           type: boolean
 *         isMenu:
 *           type: boolean
 *         order:
 *           type: number
 *         parentId:
 *           type: string
 *         children:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/MenuTree'
 *         ancestors:
 *           type: array
 *           items:
 *             type: string
 */

/**
 * @swagger
 * /addmenu:
 *   post:
 *     tags: [菜单管理]
 *     summary: 添加菜单
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *             properties:
 *               name:
 *                 type: string
 *               path:
 *                 type: string
 *               alias:
 *                 type: string
 *               disabled:
 *                 type: boolean
 *               isMenu:
 *                 type: boolean
 *               order:
 *                 type: number
 *               pid:
 *                 type: string
 *     responses:
 *       200:
 *         description: 添加成功
 */

/**
 * @swagger
 * /putmenu:
 *   post:
 *     tags: [菜单管理]
 *     summary: 更新菜单
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - id
 *               - name
 *             properties:
 *               id:
 *                 type: string
 *               name:
 *                 type: string
 *               path:
 *                 type: string
 *               alias:
 *                 type: string
 *               disabled:
 *                 type: boolean
 *               isMenu:
 *                 type: boolean
 *               order:
 *                 type: number
 *               pid:
 *                 type: string
 *     responses:
 *       200:
 *         description: 更新成功
 */

/**
 * @swagger
 * /getmenud:
 *   post:
 *     tags: [菜单管理]
 *     summary: 获取菜单详情
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - id
 *             properties:
 *               id:
 *                 type: string
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Menu'
 */

/**
 * @swagger
 * /getmenulist:
 *   get:
 *     tags: [菜单管理]
 *     summary: 获取菜单树
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/MenuTree'
 */

/**
 * @swagger
 * /delmenu:
 *   post:
 *     tags: [菜单管理]
 *     summary: 删除菜单
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - id
 *             properties:
 *               id:
 *                 type: string
 *     responses:
 *       200:
 *         description: 删除成功
 */

/**
 * @swagger
 * /addbtnrole:
 *   post:
 *     tags: [菜单管理]
 *     summary: 添加按钮权限
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - menuId
 *               - color
 *               - name
 *               - code
 *             properties:
 *               menuId:
 *                 type: string
 *               color:
 *                 type: string
 *               name:
 *                 type: string
 *               code:
 *                 type: string
 *     responses:
 *       200:
 *         description: 添加成功
 */

/**
 * @swagger
 * /getbtnrole:
 *   post:
 *     tags: [菜单管理]
 *     summary: 获取按钮权限列表
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - id
 *             properties:
 *               id:
 *                 type: string
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/ButtonRole'
 */

/**
 * @swagger
 * /putbtnrole:
 *   post:
 *     tags: [菜单管理]
 *     summary: 更新按钮权限
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - data
 *               - did
 *             properties:
 *               data:
 *                 type: object
 *                 properties:
 *                   menuId:
 *                     type: string
 *                   color:
 *                     type: string
 *                   name:
 *                     type: string
 *                   code:
 *                     type: string
 *               did:
 *                 type: string
 *     responses:
 *       200:
 *         description: 更新成功
 */

/**
 * @swagger
 * /deletebtnrole:
 *   post:
 *     tags: [菜单管理]
 *     summary: 删除按钮权限
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - id
 *               - did
 *             properties:
 *               id:
 *                 type: string
 *               did:
 *                 type: string
 *     responses:
 *       200:
 *         description: 删除成功
 */